package com.tiancheng.trade.order.controller.admin;

import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.core.utils.JsonUtil;
import com.tiancheng.trade.order.service.OrderService;
import com.tiancheng.trade.order.service.SubOrderService;
import com.tiancheng.trade.order.vo.admin.QueryOrdersVO;
import com.tiancheng.trade.order.vo.admin.QueryTotalOrderVO;
import com.tiancheng.trade.order.vo.admin.SubOrderStatusFlowVO;
import com.tiancheng.trade.order.vo.admin.res.OrderDetailResVO;
import com.tiancheng.trade.order.vo.admin.res.OrderResVO;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/***
 * @description: 管理端主订单操作
 * @date: 2024-02-22 10:24
 * @auther: liudong
 *
 */
@RestController
@RequestMapping("/admin/order")
@Slf4j
@Validated
public class OrderAdminController {

	@Autowired
	private OrderService orderService;

	@Autowired
	private SubOrderService subOrderService;

	/***
	 * @description: 主订单查询
	 * @param orderVO
	 * @return:
	 * @date: 2024-02-22 10:58
	 * @auther: liudong
	 *
	 */
	@GetMapping("/findOrderList")
	public Result queryOrderList(QueryOrdersVO orderVO) {
		PageInfo<OrderResVO> pageInfo = orderService.queryOrderList(orderVO);
		return Result.success(pageInfo);
	}

	/**
	 * 总订单导出
	 */
	@PostMapping("/exportOrderList")
	public void exportOrderList(@RequestBody QueryOrdersVO orderVO, HttpServletResponse response) throws IOException {
		orderService.exportOrderList(orderVO, response);
	}

	/***
	 * @description: 管理端获取单笔订单
	 * @param orderId
	 * @return:
	 * @date: 2024-02-22 11:06
	 * @auther: liudong
	 *
	 */
	@GetMapping("/{orderId}")
	public Result findOrderDetailsByOrderId(@PathVariable String orderId) {
		log.info("管理端获取单笔订单 para:{}", orderId);
		OrderDetailResVO detailVO = orderService.queryOrderDetailsByOrderId(orderId);
		log.info("管理端获取单笔订单 返回data:{}", JsonUtil.toJsonString(detailVO));
		return Result.success(detailVO);
	}

	/**
	 * 查询订单已支付的总金额和数量
	 * @param queryTotalOrderVO
	 * @return
	 */
	@GetMapping(value = "/queryTotal")
	public Result queryTotal(QueryTotalOrderVO queryTotalOrderVO) {
		return Result.success(orderService.queryTotal(queryTotalOrderVO));
	}

	/**
	 * 按照业务渠道统计订单的金额和数量
	 * @param queryTotalOrderVO
	 * @return
	 */
	@GetMapping(value = "/querySourceTotal")
	public Result querySourceTotal(QueryTotalOrderVO queryTotalOrderVO) {
		return Result.success(orderService.queryTotalForSource(queryTotalOrderVO));
	}

	/**
	 * 测试用
	 */
	@PostMapping("/testSubOrderStatusChange")
	public Result testSubOrderStatusChange(@RequestBody @Valid SubOrderStatusFlowVO statusFlowVO) {
		subOrderService.statusFlow(statusFlowVO.getOrderId(), statusFlowVO.getSubOrderId().stream().toArray(String[]::new),statusFlowVO.getBizStatus());
		return Result.success(statusFlowVO);
	}
}
